/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.acts;

import com.hiddenequity.utils.AppPreferences;
import com.hiddenequity.utils.AuthService;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Act_Login extends Act_Super {

	private ListView lView;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.super_operation_layout);
        
        ((ViewStub) findViewById(R.id.stub_login_operation)).inflate();

		title.setText(R.string.title_login);
		sub_title.setVisibility(View.GONE);
		filter.setVisibility(View.GONE);

        Button btnStartOperation = (Button) findViewById(R.id.btnStartOperation);
        btnStartOperation.setText(R.string.btn_add_acount);
        
        lView = (ListView) findViewById(R.id.list_googleAccount);
        populateAccountList();
    }//onCreate

    
    
    /*
     * (non-Javadoc)
     * @see android.app.Activity#onResume()
     */
    @Override
    public void onResume(){
    	super.onResume();
    	populateAccountList();
     }//onResume
    
    
    
    /*
     * Populate account list
     */
    //private int accountId;
    private void populateAccountList(){
        
    	new AuthService(this, getApplication());
    	
        ArrayAdapter m_adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, AuthService.accountNames);
        lView.setAdapter(m_adapter);

        lView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
            		int position, long id) {
            	AppPreferences.SavePreferenceInt("accountNumber", position, getApplication());
            	finishActivityWithResult(RESPONSE_GOTO_MAIN_ACTIVITY);
            }
        });	
    }//populateAccountList
    
    
    
    /*
     * Override back press button(non-Javadoc)
     * @see android.app.Activity#onBackPressed()
     */
    @Override
    public void onBackPressed() {
    	finishActivityWithResult(RESPONSE_FINISH_REQUIRED);
    }//onBackPressed

    
    
    /*
     * Add account
     */
    public void onClick_saveData(View v){
    	startActivityForResult(new Intent(Settings.ACTION_ADD_ACCOUNT), 0);
    }//onClick_saveData
    
    
    
    /*
     * Handle activity result
     * (non-Javadoc)
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);	
		populateAccountList();
	}// onActivityResult
    
}